home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 7034 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.9 KB  |  98 lines

  1. Path: solon.com!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c,comp.lang.c.moderated
  4. Subject: Re: Can someone tell me why C can't support the following
  5. Date: 17 Feb 1996 12:04:15 -0600
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Sender: clc@solutions.solon.com
  8. Approved: clc@solutions.solon.com
  9. Message-ID: <4g55av$asn@solutions.solon.com>
  10. References: <4g27n5$qa4@solutions.solon.com>
  11. NNTP-Posting-Host: solutions.solon.com
  12.  
  13. In article <4g27n5$qa4@solutions.solon.com>,
  14. Alan Krantz <atk@mathcs.emory.edu> wrote:
  15. >
  16. >I know the following is illegal but I would really really like to know
  17. >why C can't support the following construct:
  18. >
  19. >typedef struct _mytbl {
  20.  
  21. This begins a type declaration. So far so good...
  22.  
  23. >  char  *str;          ; Leagal
  24. >  int   *data;         ; Illegal (see below)
  25.  
  26. Nothing illegal about a char * structure member.
  27.  
  28. >} mytbl[] = {"Not Defined", {0},
  29. >             "Square",      {1,2,3,4,0},
  30. >             "Circle",      {1,2,0},
  31.  
  32. oops! Now you are declaring storage. You can't declare storage inside a type
  33. definition! 
  34.  
  35. >};
  36. >
  37. >Note1: I want data to point to an array of integer who's length is determined
  38. >by a {} list. I could do something silly like "int data[10]" and assume all
  39. >lists have 10 or fewer elements. 
  40. >
  41. >NOTE2:  I am NOT asking the compiler to generate a variable size structure
  42.  
  43. You sure aren't. You are asking the compiler to generate an error. What you
  44. have is not a C construct.
  45.  
  46.  
  47. >called mytbl where data varies in size. I AM asking it to allocate a vector
  48. >of integer and assign the address of that integer to data. Wouldn't this be
  49. >a nice feature....
  50.  
  51. The C language has such a feature. I have _statically_ declared entire _trees_!
  52.  
  53. The correct way to do it is to remove the typedef. Either that, or do the
  54. typedef like this:
  55.  
  56. typedef struct _mytbl {
  57.     char *str;
  58.     int *data;
  59. } mytbl_type;
  60.  
  61. int p[] = { 2, 3, 5, 7, 11, 13, 17, 19, 23 }; /* vector of integers */
  62.  
  63. mytbl_type mytbl[] = {
  64.     { "First Prime", &p[0] },  /* string, address of integer */
  65.     { "Second Prime", &p[1] },
  66.     { "Third Prime", &p[3] },
  67.     /* ... */
  68.     { "Ninth Prime", &p[8] },
  69. };
  70.  
  71. This brings us to the real point of your question. Wouldn't it be nice if we
  72. could do:
  73.  
  74.     int *x = 3;  ?
  75.  
  76. In short, no---it wouldn't buy you anything. It is allowed for string literals
  77. because C variables are defined in terms of storage units, and no atomic
  78. variable consists of enough storage cells to hold arbitrary strings. Thus we
  79. have a mechanism to create an unnamed piece of storage and statically bind it
  80. to a pointer. Doing so for integer literals would not be very useful.
  81.  
  82. Even if it were (and there are arguably many useful things that C _could_
  83. have), it would be difficult to draft into existing standards. 
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93. >Well send reasons why this is bad (and flames) to atk@cs.colorado.edu since
  94. >I don't read the news group!
  95.  
  96. Then don't write to it.
  97. -- 
  98.